package com.example.producer.config;

import org.springframework.amqp.core.*;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;

import java.util.HashMap;
import java.util.Map;

@Configuration
public class TTLRabbitmqConfig {
    /**
     * 死信队列 交换机标识符
     */
    private static final String DEAD_LETTER_QUEUE_KEY = "x-dead-letter-exchange";
    /**
     * 死信队列交换机绑定键标识符
     */
    private static final String DEAD_LETTER_ROUTING_KEY = "x-dead-letter-routing-key";

   @Bean
    public Queue TTLReDirectQueue() {
        //一般设置一下队列的持久化就好,其余两个就是默认false
        return new Queue("TTLReDirectQueue", true);
    }
    @Bean
    public Queue TTLDirectQueue() {
        Map<String, Object> args = new HashMap<>(3);
//       x-dead-letter-exchange    声明  死信交换机
        args.put(DEAD_LETTER_QUEUE_KEY, "TTLReDirectExchange");
//       x-dead-letter-routing-key    声明 死信路由键
        args.put(DEAD_LETTER_ROUTING_KEY, "dead");
        //过期时间
        args.put("x-message-ttl", 5000);
        return QueueBuilder.durable("TTLDirectQueue").withArguments(args).build();
//        return new Queue("TTLDirectQueue", true);
    }
    @Bean
    DirectExchange TTLDirectExchange() {
        return new DirectExchange("TTLDirectExchange", true, false);
    }

    @Bean
    Binding bindingTTLDirect() {
        return BindingBuilder.bind(TTLDirectQueue()).to(TTLDirectExchange()).with("ttl");
    }
    @Bean
    DirectExchange TTLReDirectExchange() {
        return new DirectExchange("TTLReDirectExchange", true, false);
    }
    @Bean
    Binding bindingTTLReDirect() {
        return BindingBuilder.bind(TTLReDirectQueue()).to(TTLReDirectExchange()).with("dead");
    }
}
